// var Utils = require('../../utils/utils');
// var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');

// var qqmapsdk = new QQMapWX({
// 	key: 'UDUBZ-OZQ3F-IGWJH-JVQLK-SI5J2-UPFAU' //在这里输入你在腾讯位置服务平台申请的KEY
// });
var timer;

const mybehavior = require("../../../../behaviors/requests")
Page({
	behaviors: [mybehavior],
	data: {
		fs_upload_url: "https://logisticsengineering.gree.com/scene_api/photo_manage",
		device: 'back',
		flash: '',
		date: "",
		address: "珠海市香洲区前山街道-珠海格力电器股份有限公司",
		addressName: "",
		cameraWidth: 0,
		cameraHeight: 0,
		w: 0,
		h: 0,
		uploader: [],
		tempImagePath_show: true,
		deletable_flag:true
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {
		const systemInfo = wx.getSystemInfoSync();
		const screenWidth = systemInfo.screenWidth;
		const screenHeight = systemInfo.screenHeight;
		const statusBarHeight = systemInfo.statusBarHeight;
		const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
		const cameraWidth = screenWidth;
		const cameraHeight = screenHeight - statusBarHeight - menuButtonInfo.height - (menuButtonInfo.top -
			systemInfo.statusBarHeight) * 2 - 90;

		console.log(systemInfo)
		this.setData({
			cameraWidth: cameraWidth,
			cameraHeight: cameraHeight
		});
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.getTime()
		// this.getLocation()
	},

	/**
	 * 获取当前时间
	 */
	getTime: function () {
		timer = setInterval(() => {
			let timeData = this.formatNowTime()
			this.setData({
				date: timeData,
			})
		}, 1000)
	},

	/**
	 * 获取地址信息
	 */

	/**
	 * 拍摄事件
	 */
	takePhoto: function () {
		const ctx = wx.createCameraContext()
		ctx.takePhoto({
			quality: 'normal',
			success: async (res) => {
				console.log(res)
				this.setData({
					w: res.width,
					h: res.height,
					tempImagePath: res.tempImagePath
				})
				// 先图片内容安全检测
				// let checkResult = await this.checkImage(imageUrl)
				// if(checkResult==0){}

				let addWatermark = await this.addWatermark(res.tempImagePath)
				let upload = [{
					url: addWatermark,
					isImage: true,

				}]

				// this.myUploadFile(addWatermark)

				// wx.previewImage({
				// 	urls: [addWatermark],
				// })
				this.setData({
					tempImagePath_show: false,
					uploader: upload
				})
			}
		})
	},

	myUploadFile(file) {
		let self = this
		wx.uploadFile({
			url: self.data.fs_upload_url + '/fs_upload',
			filePath: file,
			name: 'file',
			formData: {},
			success(res) {
				const result = JSON.parse(res.data);
				console.log(result);
				wx.hideLoading();
				if (result.code == 20000) {
					const uploader = self.data.uploader;
					let obj =
					{
						url: self.data.fs_upload_url + '/fs_download?file_id=' + result.data.file_id,
						isImage: true,
						file_id: result.data.file_id
					}
					uploader.push(obj)
					self.setData({
						tempImagePath_show: false,
						uploader: uploader
					})
					if (self.data.uploader.length < self.data.img_count) {
						self.setData({
							choose_img_btn_disabled: false
						})
					}
				} else {
					if (self.data.uploader.length < self.data.img_count) {
						self.setData({
							choose_img_btn_disabled: false
						})
					}
					wx.showToast({
						title: msg,
						icon: 'error'
					})
				}
				wx.hideLoading()

			},
			fail(err) {
				console.log(err);
			}
		})
	},
	addWatermark: function (imageUrl) {
		console.log(imageUrl)
		return new Promise((resolve, reject) => {
			wx.showLoading({
				title: '图片生成中...',
			})
			let self = this
			wx.getImageInfo({
				src: imageUrl,
				success: function (res) {
					console.log(res)
					var width = res.width, height = res.height;
					self.setData({//设定画布的宽高
						w: width,
						h: height
					})
					//获取当前时间
					let roleNameInfo = '拍摄时间：' + self.formatNowTime()
					let address = '珠海市香洲区前山街道-珠海格力电器股份有限公司'
					//创建canvas
					const ctx = wx.createCanvasContext('firstCanvas');
					ctx.drawImage(res.path, 0, 0, width, height); //先画出图片
					//将声明的时间放入canvas

					ctx.setFontSize(40) //注意：设置文字大小必须放在填充文字之前，否则不生效
					ctx.setFillStyle('#ffffff');
					ctx.fillText(roleNameInfo, 80, height - 60);
					ctx.fillText(address, 80, height - 20);
					ctx.fillText('拍摄人：' + wx.getStorageSync('name'), 80, height - 100);

					ctx.draw(false, function () {
						setTimeout(function () {
							//绘画完成回调
							//生成图片
							wx.canvasToTempFilePath({
								quality: 1,
								fileType: 'jpg',
								canvasId: 'firstCanvas',
								success: function (ress) {
									wx.hideLoading()
									resolve(ress.tempFilePath);
									// console.log(ress.tempFilePath);//ress.tempFilePath就是带有水印的图片路径
								}, fail() {
									wx.hideLoading()
									reject(new Error('转换为图片失败'));
								}
							})
						}, 600)

					})
				}
			})
		});
	},
	/**



	/**
	 * 切换摄像头
	 */
	setDevice: function () {
		this.setData({
			device: this.data.device == 'back' ? 'front' : 'back'
		})
		let text = this.data.device == 'back' ? '后置' : "前置";
		wx.showToast({
			title: "摄像头" + text
		})
	},

	/**
	 * 闪光灯开关
	 */
	setFlash: function () {
		this.setData({
			flash: this.data.flash == 'torch' ? 'off' : 'torch'
		})
	},

	/**
	 * 选择位置信息
	 */



	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {
		clearInterval(timer)

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	},
	onShareTimeline() {

	}
})